﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using Xoohoo.Extensions;

namespace Xoohoo.Utilities.Cryptography
{
    /// <summary>
    /// DES加密解密算法
    /// </summary>
    public static class DES
    {
        private static string _defaultKey = "$uio@5%8*";

        #region 加密

        public static byte[] EncryptToByteArray(byte[] inputByteArray,string key=null)
        {
            key = !key.IsNullOrEmpty() && key.Length >= 8 ? key.Substring(0, 8) : DES._defaultKey;
            byte[] keyBytes = Encoding.UTF8.GetBytes(key);
            byte[] keyIV = keyBytes;

            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return mStream.ToArray();
        }
        public static byte[] EncryptToByteArray(string encryptString, string key = null)
        {
            if (encryptString.IsNullOrWhiteSpace()) return null;

            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);

            return EncryptToByteArray(inputByteArray, key);
        }
        public static string Encrypt(string encryptString, string key = null)
        {
            if (encryptString.IsNullOrWhiteSpace()) return null;

            byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);

            return Encrypt(inputByteArray,key);
        }
        public static string Encrypt(byte[] inputByteArray, string key = null)
        {
            return Convert.ToBase64String(EncryptToByteArray(inputByteArray,key));
        }

        #endregion

        #region 解密

        public static byte[] DecryptToByteArray(byte[] inputByteArray, string key = null)
        {
            key = !key.IsNullOrEmpty() && key.Length >= 8 ? key.Substring(0, 8) : DES._defaultKey;
            byte[] keyBytes = Encoding.UTF8.GetBytes(key);
            byte[] keyIV = keyBytes;
            DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
            MemoryStream mStream = new MemoryStream();
            CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write);
            cStream.Write(inputByteArray, 0, inputByteArray.Length);
            cStream.FlushFinalBlock();
            return mStream.ToArray();
        }
        public static byte[] DecryptToByteArray(string decryptString, string key = null)
        {
            byte[] inputByteArray = Convert.FromBase64String(decryptString);
            return DecryptToByteArray(inputByteArray,key);
        }
        public static string Decrypt(string decryptString, string key = null)
        {
            return Encoding.UTF8.GetString(DecryptToByteArray(decryptString,key));
        }
        public static string Decrypt(byte[] inputByteArray, string key = null)
        {
            return Encoding.UTF8.GetString(DecryptToByteArray(inputByteArray,key));
        }
       
        #endregion
    }
}
